﻿Imports MachineManager.modGlobal

Public Class AuditLog
#Region "Constant"
  Public Const MachineWorkflow As String = "Machine Workflow"
  Public Const KEYENTER As String = "Press-Enter"
  Public Const MACHINE_MANAGEMENT As String = "Machine Management"
#End Region

  Public Shared Sub TrackLog(ByVal sAction As String, Optional ByVal sOptionalAction As String = "")
    Dim sArea As String = GetAreaWork(sAction, sOptionalAction)
    sAction = GetAction(sAction)
    If String.IsNullOrEmpty(modGlobal.GetAccountname()) Then
      Throw New Exception("User name is null or empty.")
    End If
    AddLog(sArea, sAction)
  End Sub

  Public Shared Sub TrackLog(ByVal sActiveTab As String, ByVal objControl As Control, Optional ByVal sOptionalAction As String = "")
    Dim sAction As String = GetAction(objControl, sOptionalAction)
    Dim sArea As String = GetAreaWork(sActiveTab, objControl, sOptionalAction)
    If String.IsNullOrEmpty(modGlobal.GetAccountname()) Then
      Throw New Exception("User name is null or empty.")
    End If
    AddLog(sArea, sAction)
  End Sub

  Private Shared Function GetAction(ByVal objControl As Control, Optional ByVal sOptionalAction As String = "") As String
    Return IIf(False, sOptionalAction, objControl.Text.Replace("&", "").Replace("*", "").Trim)
  End Function
  Private Shared Function GetAction(ByVal ActionString As String) As String
    ActionString = IIf(ActionString.Contains(KEYENTER), KEYENTER, ActionString)
    ActionString = IIf(ActionString.Contains("Double-Click"), "Double-Click", ActionString)
    Return ActionString
  End Function

  Private Shared Function GetAreaWork(ByVal sAction As String, Optional ByVal sOptionalAction As String = "") As String
    Dim sArea As String = sOptionalAction
    Return sArea
  End Function

  Private Shared Function GetAreaWork(ByVal sActiveTab As String, ByVal objControl As Control, Optional ByVal sOptionalAction As String = "") As String
    Dim sArea As String = sActiveTab

    'Get Form Parent Control
    Dim ParentControl As Control = objControl.Parent
    While ParentControl IsNot Nothing AndAlso Not (TypeOf ParentControl Is Form)
      ParentControl = ParentControl.Parent
    End While

    If ParentControl IsNot Nothing AndAlso IsNothing(ParentControl.Parent) Then
      'Case: Are working is Libraries or Systemt Utilities
      For i As Integer = 1 To Application.OpenForms.Count - 1
        If Not Application.OpenForms(i).IsMdiChild And Application.OpenForms(i).Name <> "frmProgressDialog" Then
          sArea += " - " + Application.OpenForms(i).Text
        End If
      Next
    Else
      'Case: Are working is Machine Workflow
      sArea = "Machine Workflow"
      For i As Integer = 1 To Application.OpenForms.Count - 1
        'Get Title Form
        sArea += " - " + Application.OpenForms(i).Text
      Next
      'Get Title tabpage
    End If
    Return sArea
  End Function

  Public Shared Function AddLog(ByVal sArea As String, ByVal sAction As String) As Boolean
    Try
      Dim DataBaseAccessObj As DataBaseAccess = New DataBaseAccess

      Dim sSqlQuery As String = "SELECT * FROM tbl_Log"
      Dim dtable As DataTable = DataBaseAccessObj.GetDataTable(sSqlQuery)
      Dim dRow As DataRow = dtable.NewRow
      SetDataTable(dRow, sArea, sAction)
      dtable.Rows.Add(dRow)
      Return DataBaseAccessObj.UpdateDataTable(sSqlQuery, dtable)
    Catch ex As Exception
      Throw New Exception("Can not insert Record Log!")
      Return False
    End Try
  End Function

  Private Shared Sub SetDataTable(ByRef drow As DataRow, ByVal sArea As String, ByVal sAction As String)
    drow("ID") = GetMaxLogID() + 1
    drow("Time") = Now.ToLongTimeString()
    drow("Date") = DateTime.Now.Date
    drow("Action") = sAction
    drow("AccountID") = modGlobal.GetAccountID()
    drow("Area") = sArea
  End Sub

  Public Shared Function GetMaxLogID() As Integer
    Dim DataBaseAccessObj As DataBaseAccess = New DataBaseAccess()
    Dim DataTable As DataTable = New DataTable()
    Dim openDB As Boolean = False
    Dim MySQLString As String = ""
    Try
      openDB = DataBaseAccessObj.OpenConnection()
      If (openDB) Then
        MySQLString = "SELECT * FROM tbl_Log"

        If (DataBaseAccessObj.IsEmpty(MySQLString)) Then
          Return 0
        Else
          DataTable = New DataTable()
          DataTable = DataBaseAccessObj.GetDataTable(MySQLString)
          Dim MaxRow As Integer = DataTable.Rows.Count
          ' Return Max Log ID
          Return Convert.ToInt16(DataTable.Rows(MaxRow - 1).Item(0).ToString())
        End If

      End If
    Catch ex As Exception
      'MessageBox.Show(My.Settings.IDS_CONNECT_DATABASE_ERROR & " " & ex.Message.ToString(), My.Settings.IDS_MACHINE_MANAGEMENT, MessageBoxButtons.OK, MessageBoxIcon.Error)

      ' Return Null
      Return -1
    Finally
      DataBaseAccessObj.CloseConnection()
    End Try
  End Function

End Class
